package com.zyh.server.rabbitmq;

import com.zyh.server.config.MQConfig;
import com.zyh.server.entity.MiaoshaMsg;
import com.zyh.server.entity.User;
import com.zyh.server.service.MiaoshaService;
import com.zyh.server.utils.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zyh
 * @Date: 2024-11-20-15:36
 * @Description 消息接收者
 */
@Slf4j
@Service
public class MQReceiver {
	@Autowired
	private MiaoshaService miaoshaService;

	// 消息监听
	@RabbitListener(queues = MQConfig.miaosha_queue)
	public void receive(String msg) {
		// 将接收到的消息转为对象
		MiaoshaMsg miaoshaMsg = BeanUtil.stringToBean(msg, MiaoshaMsg.class);
		log.info("接收到消息：{}", miaoshaMsg);
		// 获取用户信息
		User user = miaoshaMsg.getUser();
		Integer miaoshaGoodsId = miaoshaMsg.getMiaoshaGoodsId();
		log.info("用户id：{}，秒杀商品id：{}", user.getId(), miaoshaGoodsId);
		// 执行秒杀方法
		miaoshaService.miaoSha(user,miaoshaGoodsId);
	}
}
